const {ccclass, property} = cc._decorator;

@ccclass
export default class PengGangs extends cc.Component {

    

    onLoad() {
        if(!cc.vv){
            return;
        }
        
        const gameChild = this.node.getChildByName('game');
        const myself = gameChild.getChildByName('myself');
        const penggangroot = myself.getChildByName('penggangs');
        const realwidth = cc.director.getVisibleSize().width;
        const scale = realwidth / 1280;
        penggangroot.scaleX *= scale;
        penggangroot.scaleY *= scale;

        this.node.on(cc.vv.game_event_map.PENG_NOTIFY, (data) => {
            this.onPengGangChanged(data.detail);
        });

        this.node.on(cc.vv.game_event_map.GANG_NOTIFY, (data) => {
            this.onPengGangChanged(data.detail.seatData);
        });

        this.node.on(cc.vv.game_event_map.GAME_BEGIN, (data) => {
            this.onGameBegin();
        });
    }


    /**碰杠更新 */
    onPengGangChanged(seatDate: cc.seat){
        if(seatDate.anGangs == null && seatDate.dianGangs == null && seatDate.wanGangs == null && seatDate.pengs == null){
            return;
        }
        const localIndex = cc.vv.gameNetMgr.getLocalIndex(seatDate.seatIndex);
        const side = cc.vv.mahjongmgr.getSide(localIndex);
        const pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        console.log('%conPengGangChanged' + localIndex, cc.vv.logStyle);

        const gameChild = this.node.getChildByName('game');
        const myself = gameChild.getChildByName(side);
        const penggangroot = myself.getChildByName('penggangs');

        for(let i = 0; i < penggangroot.childrenCount; i++){
            penggangroot.children[i].active = false;
        }
        
        // 初始化杠牌
        let index = 0;
        let gangs = seatDate.anGangs;
        for(let i = 0; i < gangs.length; i++){
            const mjid = gangs[i];
            this.initPengAndGangs(penggangroot, side, pre, index, mjid, 'angang');
            index++;
        }
        
        gangs = seatDate.dianGangs;
        for(let i = 0; i < gangs.length; i++){
            const mjid = gangs[i];
            this.initPengAndGangs(penggangroot, side, pre, index, mjid, 'diangang');
            index++;
        }

        gangs = seatDate.wanGangs;
        for(let i = 0; i < gangs.length; i++){
            const mjid = gangs[i];
            this.initPengAndGangs(penggangroot, side, pre, index, mjid, 'wangang');
            index++;
        }

        // 初始化碰牌
        gangs = seatDate.pengs;
        for(let i = 0; i < gangs.length; i++){
            const mjid = gangs[i];
            this.initPengAndGangs(penggangroot, side, pre, index, mjid, 'peng');
            index++;
        }

    }


    initPengAndGangs(penggangroot: cc.Node, side: string, pre: string, index: number, mjid, flag: string){
        let pgroot: cc.Node;
        if(penggangroot.childrenCount <= index){
            if(side == cc.vv.directions.left || side == cc.vv.directions.right){
                pgroot = cc.instantiate(cc.vv.mahjongmgr.pengPrefabLeft);
            }
            else{
                pgroot = cc.instantiate(cc.vv.mahjongmgr.pengPrefabSelf);
            }
            penggangroot.addChild(pgroot);
        }
        else{
            pgroot = penggangroot.children[index];
            pgroot.active = true;
        }

        switch(side){
            case cc.vv.directions.left:
                pgroot.y = -(index * 25 * 3);
                break;
            case cc.vv.directions.right:
                pgroot.y = index * 25 * 3;
                pgroot.setLocalZOrder(-index);
                break;
            case cc.vv.directions.myself:
                pgroot.x = index * 25 * 3 + index * 10;
                break;
            default:
                pgroot.x = -(index * 55 * 3);
        }

        const sprites = pgroot.getComponentsInChildren(cc.Sprite);
        for(let i = 0; i < sprites.length; i++){
            const sprite = sprites[i];
            if(sprite.node.name == 'gang'){
                const isGang = flag != 'peng';
                sprite.node.active = isGang;
                sprite.node.scaleX = 1.0;
                sprite.node.scaleY = 1.0;
                if(flag == 'angang'){
                    sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
                    if(side == cc.vv.directions.myself || side == cc.vv.directions.up){
                        sprite.node.scaleX = 1.4;
                        sprite.node.scaleY = 1.4;
                    }
                }
                else{
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
                }
            }
            else{
                sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
            }
        }
    }



    onGameBegin(){
        for(let key in cc.vv.directions){
            this.hideSide(cc.vv.directions[key]);
        }
    }



    hideSide(side: string){
        const gameChild = this.node.getChildByName('game');
        const myself = gameChild.getChildByName(side);
        const penggangroot = myself.getChildByName('penggangs');
        if(penggangroot){
            for(let i = 0; i < penggangroot.childrenCount; i++){
                penggangroot.children[i].active = false;
            }
        }
    }
}
